//
// Created by shiminli on 2024/1/3.
//

#include "registers.h"

/**
CREATE TABLE `registers` (
  `user_id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `device_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ip` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `platform` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `account_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `mode` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `create_time` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5635818456 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 */
RegistersColumnName registers::names;
long registers::insert(Registers &data) {
    string cmd = "INSERT INTO ";
    cmd.append(names.tabName);
    cmd.append(" ( ").append(names.device_id).append(",").append(names.ip).append(",")
            .append(names.platform).append(",").append(names.account_type).append(",").append(names.mode).append(",")
            .append(names.create_time).append(" ) VALUES ( ");
    cmd.append("'").append(data.device_id).append("', ");
    cmd.append("'").append(data.ip).append("', ");
    cmd.append("'").append(data.platform).append("', ");
    cmd.append("'").append(data.account_type).append("', ");
    cmd.append("'").append(data.mode).append("', ");
    // %Y-%m-%d %H:%i:%s:%f'
    cmd.append("STR_TO_DATE('").append(data.create_time).append("','%Y-%m-%d %H:%i:%s.%f')");
    cmd.append(")");
    int ret = SQLHelper::getInstance()->executeUpdate(cmd.c_str());
    if (ret != 0)return -1;
    return SQLHelper::getInstance()->getLastInsertId();
}
